//
//  VideoPlayCelll.swift
//  Chufang
//
//  Created by zhu xietong on 2017/9/21.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import UIKit
import Eelay

fileprivate var _paly:UIImage? = nil
fileprivate var _pause:UIImage? = nil
fileprivate var _zoomin:UIImage? = nil
fileprivate var _zoomout:UIImage? = nil

public class VideoIMG{
    
    public static var play = UIImage(named: "play", in: Bundle(for: VideoPlayCelll.self), compatibleWith: nil)
    public static var pause = UIImage(named: "pause", in: Bundle(for: VideoPlayCelll.self), compatibleWith: nil)
    public static var zoomin = UIImage(named: "btn_zoom_in", in: Bundle(for: VideoPlayCelll.self), compatibleWith: nil)
    public static var zoomout = UIImage(named: "btn_zoom_out", in: Bundle(for: VideoPlayCelll.self), compatibleWith: nil)

  
}


open class VideoPlayCelll:JoTableCell,CellVideoSupport{
    
    public let coverV = UIImageView()
    public let playBt = UIButton()
    
    public weak var player:XLVideoPlayer?
        {
        didSet{
            if let _player = player
            {
                _player.frame = coverV.frame
                contentView.addSubview(_player)
            }
            
        }
    }
    
    open override func addLayoutRules() {
        let imgH = (Swidth)*0.5625

        contentView.eelay = [
            [coverV,[ee.T.L.B.R],"\(imgH)"],
            [playBt,[ee.X.Y],"60",60]
        ]
        let img = UIImage(named: "play", in: Bundle(for: VideoPlayCelll.self), compatibleWith: nil)
        playBt.setImage(img, for: .normal)
        playBt.addTarget(self, action: #selector(playAction), for: .touchUpInside)
    }
    
    open override func mo_model_reload() {
        coverV.image = UIImage(named: mo.model["cover",""])
    }
    

    @objc func playAction() {

        if let listCtr = self.delegate as? ListVideoSupport
        {
            
            listCtr.playVideoWith(videoKit: self, cellModel: self.mo.model, isCell: true)
        }
    }
    
    open func stopVideo() {
        self.player?.removeFromSuperview()
        self.player?.destroy()
        self.player = nil
    }

    
    
//    open override func cellNodeVisibilityEvent(_ event: ASCellNodeVisibilityEvent, in scrollView: UIScrollView?, withCellFrame cellFrame: CGRect) {
//        if cellFrame == .zero
//        {
//
//            self.player?.removeFromSuperview()
//            self.player?.destroy()
//            self.player = nil
//        }
//    }
    
}
